package com.rosh.rpc.core.netty.codec;

import com.rosh.rpc.core.data.RpcResponse;
import com.rosh.rpc.core.utils.ProtostuffUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

/**
 * @Description: RpcResponse编码器，输入RpcResponse,输出ByteBuf
 * @Author: rosh
 * @Date: 2022/6/12 22:32
 */
public class RpcResponseEncoder extends MessageToMessageEncoder<RpcResponse> {


    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, RpcResponse rpcResponse, List<Object> list) throws Exception {
        byte[] bytes = ProtostuffUtil.serialize(rpcResponse);
        ByteBuf buffer = channelHandlerContext.alloc().buffer(bytes.length);
        buffer.writeBytes(bytes);
        list.add(buffer);
    }
}
